package cn.nfwx.tv.widget.seekbar;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;

import java.util.ArrayList;
import java.util.List;

import cn.nfwx.tv.R;
import cn.nfwx.tv.adapter.ClipImageAdapter;
import cn.nfwx.tv.util.LogUtil;
import cn.nfwx.tv.widget.seekbar.RangeSeekBar;


/**
 * 视频切换View
 * Created by zhouzechao on 2017/11/25.
 */
public class ClipRangeSeekBar extends FrameLayout {

    private RecyclerView clip_recyclerView;
    private RangeSeekBar rangseekbar;

    private List<Object> mtimeCodes = new ArrayList<>();
    private ClipImageAdapter mAdapter;

    public ClipRangeSeekBar(@NonNull Context context) {
        this(context, null);
    }

    public ClipRangeSeekBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ClipRangeSeekBar(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();
    }


    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_clip_range, this, true);
        clip_recyclerView = findViewById(R.id.clip_recyclerView);
        rangseekbar = findViewById(R.id.rangseekbar);


        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        clip_recyclerView.setLayoutManager(layoutManager);
        mAdapter = new ClipImageAdapter();
        clip_recyclerView.setAdapter(mAdapter);

        mtimeCodes.clear();
    }


    public void setData(List timeCodes) {
        try {
            if (timeCodes == null || timeCodes.isEmpty()) {
                return;
            }


            mtimeCodes.clear();
            mtimeCodes.addAll(timeCodes);
            LayoutParams l1 = (LayoutParams) clip_recyclerView.getLayoutParams();
            l1.setMargins(rangseekbar.leftMargin, 0, rangseekbar.rightMargin, 0);
            l1.width = rangseekbar.viewWidth;

            rangseekbar.setRules(0, timeCodes.size() - 1, timeCodes.size() / 5, 1);
            rangseekbar.setValue(0, timeCodes.size() - 1);

            int width = rangseekbar.viewWidth;
            // 计算最合适的图片长度，采用互补法
            double perWidth = width * 1f / timeCodes.size() * 1f;
            double yu = perWidth - Math.floor(perWidth);
            double a = Math.floor(timeCodes.size() * yu);
            double b = timeCodes.size() - a;
            int with = (int) (b * Math.floor(perWidth) + a * Math.ceil(perWidth));
            mAdapter.setPerWidth(width * 1f / timeCodes.size() * 1f);
            mAdapter.setCeilWidthCount(a);
            mAdapter.setLastWidth(width - with);

            mAdapter.setNewData(timeCodes);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }

    public void setOnRangeChangedListener(RangeSeekBar.OnRangeChangedListener changedListener) {
        rangseekbar.setOnRangeChangedListener(changedListener);
    }

    public void clearRangseekbar() {
        rangseekbar.setRules(0, 50, 5, 1);
        rangseekbar.setValue(0, 50);
        mtimeCodes.clear();
        mAdapter.setNewData(mtimeCodes);
    }

    /**
     * 是否显示进度
     *
     * @param status
     */
    public void setShowProgress(boolean status) {
        if (rangseekbar != null) {
            rangseekbar.setShowProgress(status);
        }
    }

    /**
     * 显示进度
     *
     * @param progress
     */
    public void setProgress(float progress) {
        rangseekbar.setProgress(progress);
        setShowProgress(true);
    }

    public void setClipPath(String clipPath) {
        mAdapter.setClipPath(clipPath);
    }

    public void release(){
        mtimeCodes.clear();
        mAdapter.getData().clear();
    }
}
